package com.example.deviceinfo.data.network

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


// 网络配置类
object NetworkConfig {
    // https://69876df4.r6.cpolar.top
    const val BASE_URL = "https://69876df4.r6.cpolar.top"  // 替换为实际 API 地址

    // 创建 GSON 解析器
    fun provideGson(): Gson = GsonBuilder().create()

    // 创建 OkHttp 客户端
    fun provideOkHttpClient(): OkHttpClient {
        val loggingInterceptor = HttpLoggingInterceptor().apply {
            level = HttpLoggingInterceptor.Level.BODY  // 开发环境打印完整请求日志
        }

        return OkHttpClient.Builder()
            .addInterceptor(loggingInterceptor)
            .build()
    }

    // 创建 Retrofit 实例
    fun provideRetrofit(gson: Gson, okHttpClient: OkHttpClient): Retrofit =
        Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build()

    // 创建 API 服务
    fun provideDeviceApiService(retrofit: Retrofit): IDeviceApi =
        retrofit.create(IDeviceApi::class.java)
}